#include <amxmodx>
#include <items>
#include <engine>
#include <fakemeta>

new PLUGIN_NAME[] 	= "UM Item: Perfect Aim"
new PLUGIN_AUTHOR[] 	= "Cheap_Suit"
new PLUGIN_VERSION[] 	= "1.0"

new bool:g_PerfectAim[33] 
public plugin_init() 
{
	register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR)
	register_item("Perfect Aim", "Perfect aim", 5000)
	register_forward(FM_TraceLine, "FW_TraceLine")
}

public client_connect(id) {
	g_PerfectAim[id] = false
}

public Enable_Item(id) {
	g_PerfectAim[id] = true
}

public Disable_Item(id) {
	g_PerfectAim[id] = false
}

public FW_TraceLine(Float:v1[3], Float:v2[3], noMonsters, id) 
{
	if(!is_user_connected(id) || !is_user_alive(id))
		return FMRES_IGNORED

	if(!g_PerfectAim[id]) {
		return FMRES_IGNORED
	}
		
	new clip, ammo, weapon = get_user_weapon(id, clip, ammo)
	if(clip < 1 || weapon == CSW_KNIFE) {
		return FMRES_IGNORED
	}
		
	new iAim[3], Float:fAim[3]
	get_user_origin(id, iAim, 3)
	IVecFVec(iAim, fAim)

	set_tr(TR_vecEndPos, fAim)

	new target, body
	get_user_aiming(id, target, body)

	if(is_valid_ent(target)) 
	{
		set_tr(TR_flFraction, 0.1)
		set_tr(TR_pHit, target)
		set_tr(TR_iHitgroup, body)
	}
	return FMRES_IGNORED
}
